import { defineStore } from 'pinia';
// @ts-ignore
import { reactive, ref } from 'vue';
import {getDefaultHospital, getHospitalInfo} from "@/api/hospital";

export const useHospitalStore  = defineStore('hospital', () => {
    const state = reactive({
        id: '',
        name: '',
        address: '',
        desc: '',
        province: '',
        city: '',
        district: '',
        hotline: '',
        latitude_val: '',
        longitude_val: '',
        photo: '',
    })

    const choseStoreId = ref(0);

    // 切换门店
    const handleChoseStore = async (storeId: any) => {
        choseStoreId.value = storeId
        await handleGetHospital()
    }

    // 获取门店信息
    const handleGetHospital = async () => {

        if (choseStoreId.value > 0) {
            const {code, data, message} = await getHospitalInfo({hospital_id: choseStoreId.value})
            if (code === 1) {
                // hospital.name = {...data}
                Object.assign(state, data)
            }
        } else {
            const {code, data, message} = await getDefaultHospital({})
            if (code === 1) {
                // hospital.name = {...data}
                Object.assign(state, data)
            }
        }
    }

    return {
        ...toRefs(state),
        handleGetHospital,
        handleChoseStore
    }
},{
    // persist: true,
    persist: {
        // 默认值，就是使用localStorage
        // storage: localStorage
        // 重写存取方法，转调给UniApp
        storage: {
            getItem(key) {
                // @ts-ignore
                return uni.getStorageSync(key)
            },
            setItem(key, val) {
                // @ts-ignore
                uni.setStorageSync(key, val)
            }
        }
    }
})
